#include <iostream>
#include <map>

using namespace std;

typedef long long LL;

int n, p;
map<string, int> mp;//<高级词汇,含金量>

bool check(char ch)
{
	return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}

int main()
{
	cin >> n >> p;
	for(int i = 1; i <= n; i++)
	{
		string s; int x;
		cin >> s >> x;
		mp[s] = x;
	}

	//读取英语作文，每读取一个英语单词的时候，就去map中查找一下含金量
	LL ret = 0;
	char ch;
	string str = "";
	while(scanf("%c", &ch) != EOF)
	{
		if(check(ch)) str += ch;
		else 
		{
			//读到分隔符
			ret = (ret + mp[str]) % p;
			str = "";
		}
	}
	cout << ret << endl;

	return 0;
}

